package edu.iss.demo.web.controller;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.iss.demo.exception.StudentNotFound;
import edu.iss.demo.exception.UserDetailNotFound;
import edu.iss.demo.model.Employee;
import edu.iss.demo.model.Student;
import edu.iss.demo.model.UserDetail;
import edu.iss.demo.service.EmployeeService;
import edu.iss.demo.service.UserDetailService;


@Controller
@RequestMapping(value = "/userdetail") //

public class UserDetailController {

	@Autowired
	private UserDetailService userDetailService;
	@Autowired
	private EmployeeService employeeService;
	
	
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView newUserDetailPage() {
		ModelAndView mav = new ModelAndView("userdetail_new", "UserDetail",new UserDetail());
		List<Employee> empList = employeeService.findAll();
		mav.addObject("empList", empList);

		return mav;
	}

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public ModelAndView createNewUser(@ModelAttribute @Valid UserDetail userDetail,
			BindingResult result, final RedirectAttributes redirectAttributes) {

		if (result.hasErrors())
			return new ModelAndView("userdetail_new");

		userDetailService.create(userDetail);
		
		ModelAndView mav = new ModelAndView();
		String message = "New User "
				+ " was successfully created.";

		mav.setViewName("AdminHome");

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView userListPage() {
		ModelAndView mav = new ModelAndView("userdetail-list");
		List<UserDetail> userdetail= userDetailService.findAll();
		mav.addObject("userCon", userdetail);
		return mav;
	}
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editUserPage(@PathVariable Long id) {
		ModelAndView mav = new ModelAndView("userdetail-edit");
		UserDetail userdetail = userDetailService.findById(id);
		mav.addObject("userdetail", userdetail);
		return mav;
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
	public ModelAndView editUserPage(@ModelAttribute @Valid UserDetail userdetail,
			BindingResult result, @PathVariable Long id,
			final RedirectAttributes redirectAttributes) throws UserDetailNotFound {

		userdetail.setUserId(id);
		if (result.hasErrors())
			return new ModelAndView("userdetail-edit");

		ModelAndView mav = new ModelAndView("AdminHome");
		String message = userdetail.getEmployeeId().getName()
				+ " was successfully updated.";


		userDetailService.update(userdetail);

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView deleteStudent(@PathVariable Integer id,
			final RedirectAttributes redirectAttributes) throws UserDetailNotFound {

		ModelAndView mav = new ModelAndView("AdminHome");

		UserDetail userdetail= userDetailService.delete(id);
		String message = "The user" + userdetail.getEmployeeId().getName()
				+ " was successfully deleted.";

		redirectAttributes.addFlashAttribute("message", message);
		return mav;
	}
	
}
